from django.http import JsonResponse
from django.shortcuts import render

from study1.context.context import get_data



def test1(request):
    return render(request, "study1/view1.html")


list1 = [1221, 454, 789, 232, 678, 888, 778, 534, 9938]
list2 = [44221, 94944, 24446, 93947, 22324, 24445, 22456, 47575]
list3 = [33341, 3224, 75676, 99990, 22114, 96864, 34356, 53422, 99607, 77474, 23353, 3556, 1, 23244]
list4 = [1121, 333, 43, 335, 3565, 32324, 553, 75, 68, 3232, 3237]
list5 = [211, 2455, 64564, 4564, 2121, 343, 5648, 5657]


def test2(req):
    try:
        ip = req.META['HTTP_X_FORWARDED_FOR'].split(",")[0]
    except:
        try:
            ip = req.META['REMOTE_ADDR']
        except:
            ip = ""
    data = {'flag': 0, 'msg': '不在这5个list中', 'data': 0}
    param = 'None'
    try:
        param = req.GET['param']
        param = int(param)
    except:
        data = {'flag': 0, 'msg': '参数错误', 'data': ''}
    print('test2:----------ip:', ip, ', param:', str(param))
    if param in list1:
        data = {'flag': 1, 'msg': 'list1中的数据', 'data': 1}
    if param in list2:
        data = {'flag': 1, 'msg': 'list2中的数据', 'data': 2}
    if param in list3:
        data = {'flag': 1, 'msg': 'list3中的数据', 'data': 3}
    if param in list4:
        data = {'flag': 1, 'msg': 'list4中的数据', 'data': 4}
    if param in list5:
        data = {'flag': 1, 'msg': 'list5中的数据', 'data': 5}
    return JsonResponse(data)


def test3(req):
    data = get_data
    try:
        ip = req.META['HTTP_X_FORWARDED_FOR'].split(",")[0]
    except:
        try:
            ip = req.META['REMOTE_ADDR']
        except:
            ip = ""
    size1 = 0
    size2 = 0
    size3 = 0
    size4 = 0
    size5 = 0
    try:
        size1 = req.POST['list1']
        size2 = req.POST['list2']
        size3 = req.POST['list3']
        size4 = req.POST['list4']
        size5 = req.POST['list5']
    except:
        data = {'flag': 0, 'msg': '参数错误', 'data': ''}
    print('test3:----------ip:', ip, ', list1:', size1, ', list2:', size2, ', list3:', size3, ', list4:', size4, ', list5:', size5)
    if size1 != str(len(list1)) or size2 != str(len(list2)) \
            or size3 != str(len(list3)) \
            or size4 != str(len(list4)) \
            or size5 != str(len(list5)):
        response = JsonResponse({'flag': 0, 'msg': '数据错误', 'data': ''}, safe=False)
        response["Access-Control-Allow-Origin"] = "*"
        response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
        response["Access-Control-Max-Age"] = "1000"
        response["Access-Control-Allow-Headers"] = "*"
        return response

    response = JsonResponse(data, safe=False)
    response["Access-Control-Allow-Origin"] = "*"
    response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
    response["Access-Control-Max-Age"] = "1000"
    response["Access-Control-Allow-Headers"] = "*"
    return response
